package com.whd.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whd.domain.ResultVo;
import com.whd.entity.User;
import com.whd.service.FileService;
import com.whd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author whd
 * @date 2023/06/26 15:58:52
 */
@RestController
public class UserController {

    @Autowired
    private FileService fileService;
    @Autowired
    private UserService userService;


    @PostMapping("user")
    public ResultVo save(User user,MultipartFile photoData, HttpServletRequest request) throws IOException {
        String path = request.getRealPath("imgs");
        String photoFile = fileService.addPhoto(path, photoData);
        user.setPhoto(photoFile);
        int rows = userService.save(user);
        if (rows > 0) {
            fileService.removePhoto(path,photoData.getOriginalFilename());
        }
        return rows>0? ResultVo.success("添加成功",user) : ResultVo.fail("添加失败");
    }
    @GetMapping("user")
    public ResultVo findAll(){
        List<User> list = userService.findAll();
        return ResultVo.success(list);
    }

    @DeleteMapping("user/{id}")
    public ResultVo delete(@PathVariable Integer id, HttpServletRequest request) {
        String path = request.getRealPath("imgs");
        User user = userService.findOne(id);

        String photoFile = user.getPhoto();
        int rows = userService.delete(id);
        if(rows>0) {
            fileService.removePhoto(path, photoFile);
        }
        return rows > 0 ? ResultVo.success("删除成功") : ResultVo.fail("删除失败");
    }
    @PutMapping("user")
    public ResultVo update(User user,MultipartFile photoData, HttpServletRequest request) throws IOException {
        String path = request.getRealPath("imgs");
        System.out.println(path+"**************************************************");
        String oldPhotoFile = userService.findOne(user.getId()).getPhoto();
        if (photoData != null) {
            String newPhotoFile = fileService.addPhoto(path, photoData);
            user.setPhoto(newPhotoFile);
        }
        int rows = userService.update(user);
        if(rows>0) {
            fileService.removePhoto(path, oldPhotoFile);
        }
        return rows > 0 ? ResultVo.success("修改成功") : ResultVo.fail("修改失败");
    }

//    @PostMapping("photo")
//    public ResultVo savePhoto(MultipartFile photoData, HttpServletRequest request) throws IOException {
//        String path = request.getRealPath("imgs");
//        String photoFile = fileService.addPhoto(path, photoData);
//        return ResultVo.success("ok",photoFile);
//    }

    @GetMapping("user/{pageNum}/{pageSize}")
    public ResultVo findByPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<User> list = userService.findByPage(pageNum, pageSize);
        return ResultVo.success("查询成功", list);
    }
}
